package com.example.qasys.controller;

import com.example.qasys.config.Result;
import com.example.qasys.entities.CookieAndToken;
import com.example.qasys.service.CodyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import okhttp3.Cookie;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

@RestController
@CrossOrigin
@RequestMapping(value = "/cody")
@Tag(name = "Cody服务")
public class CodyController {

    @Resource
    CodyService service;

    @GetMapping("/question")
    @Operation(summary = "提交问题并获取答案(含token)")
    public Result postQuestionAndGetAnswer(String question) throws IOException, InterruptedException {
        return service.postQuestionAndGetAnswer(question);
    }

    @PutMapping("/setCookieAndToken")
    @Operation(summary = "更新cookie和token")
    public Result setCookieAndToken(@RequestBody CookieAndToken cookieAndToken){
        return service.setCookieAndToken(cookieAndToken.getCookie(), cookieAndToken.getToken());
    }

    @PostMapping("/uploadTxt")
    @Operation(summary = "上传txt文件")
    public Result uploadTxt(@RequestParam("file") MultipartFile multipartFile) throws IOException {
//        File file = new File(multipartFile.getOriginalFilename());
        String originalFilename = multipartFile.getOriginalFilename();
        String filePath = "D:\\桌面\\" + originalFilename;
        multipartFile.transferTo(new File(filePath));

        return service.uploadTxt(new File(filePath));
    }

    @GetMapping
    @Operation(summary = "提交问题并获取答案(无token)")
    public Result postQuestionAndGetAnswerNoToken(String question) throws IOException, InterruptedException{
        return service.postQuestionAndGetAnswerNoToken(question);
    }

    @PostMapping("/uploadWord")
    @Operation(summary = "上传word文件")
    public Result uploadWord(@RequestParam("file") MultipartFile multipartFile) throws IOException {
//        File file = new File(multipartFile.getOriginalFilename());
        String originalFilename = multipartFile.getOriginalFilename();
        String filePath = "D:\\桌面\\" + originalFilename;
        multipartFile.transferTo(new File(filePath));

        return service.uploadWord(new File(filePath));
    }
}
